package com.mycompany.controller;

import java.util.List;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.mycompany.entity.Period;
import com.mycompany.entity.Rule;
import com.mycompany.service.PeriodService;
import com.mycompany.service.RuleService;
import com.mycompany.service.UserService;

/**
 * Handles requests for the application home page.
 */
@Controller
public class PeriodController {
	
	private static final Logger logger = LoggerFactory.getLogger(PeriodController.class);
	
	@Autowired
	PeriodService periodService;  
	
	@Autowired
	RuleService ruleService;  
	
	@Autowired
	UserService userService;
	
	@RequestMapping(value = "/admin/period", method = RequestMethod.GET)
	public String periodListHandle(Model model) {
		logger.debug("Period List here!");
		List<Period> periods = periodService.findAll();
		model.addAttribute("periods", periods);
		return "admin/period/period_list";
	}
	@RequestMapping(value = "/admin/periodSearch")
	public String periodFindHandle(Model model,@RequestParam(value = "name") String name) {
		logger.debug("periodFindHandle here!");
		Period u = new Period();
		u.setName(name);
		List<Period> periods = periodService.findListByCodition(u);
		model.addAttribute("periods", periods);
		return "admin/period/period_list";
	}
	@RequestMapping(value = "/admin/periodDelete")
	public String periodDeleteHandle(Model model,@RequestParam(value = "id") int id) {
		logger.debug("periodDeleteHandle here!");
		Period u = new Period();
		u.setPeriodId(id);
		u  = periodService.findById(id);
		periodService.remove(u);
		List<Period> periods = periodService.findAll();
		model.addAttribute("periods", periods);
		return "admin/period/period_list";
	}
	
	@RequestMapping(value = "/admin/periodEditForm")
	public String periodEditFormHandle(Model model,@RequestParam(value = "id") int id) {
		logger.debug("periodEditFormHandle here!");
		Period u = new Period();
		u.setPeriodId(id);
		u  = periodService.findById(id);
		model.addAttribute("period", u);
		model.addAttribute("action","periodEdit");
		return "admin/period/period_form";
	}
	
	@RequestMapping(value = "/admin/periodEdit")
	public String periodEditHandle(Model model,@ModelAttribute( "Period" ) @Valid Period period) {
		logger.debug("periodEditHandle here!");
		
		// set properties 
		Period u = new Period();
		u  = periodService.findById(period.getPeriodId());
		u.setName(period.getName());
		u.setDetail(period.getDetail());
		u.setStartDt(period.getStartDt());
		u.setEndDt(period.getEndDt());
		u.setWinNumberTop(period.getWinNumberTop());
		u.setWinNumberDown(period.getWinNumberDown());
		u.setWin_ThreeNumberTang(period.getWin_ThreeNumberTang());
		u.setWin_ThreeNumberTod(period.getWin_ThreeNumberTod());
		u.setWin_ThreeNumberTangDown(period.getWin_ThreeNumberTangDown());

		// set User
//		UserDetails userDetails = (UserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//		User user = new User();
//		user.setLogin(userDetails.getUsername());
//		user = userService.findListByCodition(user).get(0);
//		u.setUser(user);
		
		
		periodService.merge(u);
		u = new Period();
		u.setPeriodId(period.getPeriodId());
		List<Period> periods = periodService.findListByCodition(u);
		model.addAttribute("periods", periods);
		return "admin/period/period_list";
	}
	
	
	@RequestMapping(value = "/admin/periodAddForm")
	public String periodAddFormHandle(Model model) {
		logger.debug("periodAddFormHandle here!");
		List<Rule> rules = ruleService.findAll();
		model.addAttribute("action","periodAdd");
		model.addAttribute("rules",rules);
		return "admin/period/period_form";
	}
	
	@RequestMapping(value = "/admin/periodAdd")
	public String periodAddHandle(Model model,@ModelAttribute( "Period" ) @Valid Period period,@RequestParam(value = "ruleId") int ruleId) {
		logger.debug("periodEditHandle here!");
		period.setActiveFlg(true);
		
		
		// set Rule
		Rule rule =ruleService.findById(ruleId);
		period.setRule(rule);
		
//		// set User
//		UserDetails userDetails = (UserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//		User user = new User();
//		user.setLogin(userDetails.getUsername());
//		user = userService.findListByCodition(user).get(0);
//		period.setUser(user);
		
		
		periodService.persist(period);
		List<Period> periods = periodService.findAll();
		model.addAttribute("periods", periods);
		return "admin/period/period_list";
	}
	
}
